package socketVersion.Backend.Flights;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

import socketVersion.GenericServer;
import socketVersion.Reply;
import socketVersion.Backend.Cars.CarServer;

public class FlightServer extends GenericServer{

	private FlightServerImplementation flightServer;
	
	
	public FlightServer(int port) throws IOException {
		super(port);
		flightServer = new FlightServerImplementation();
	}

	@Override
	protected Reply handleMethod(String methodName, LinkedList<Object> args) {
		LinkedList<Object> returnValues;
		Reply reply = null;
		//In the event that parameter cast fails
		try{
			if("addFlight".equals(methodName)&&(args.size()==4)){
				int id = ((Integer)args.get(0)).intValue();
				int flightNum = ((Integer)args.get(1)).intValue();
				int flightSeats = ((Integer)args.get(2)).intValue();
				int flightPrice = ((Integer)args.get(3)).intValue();
				boolean result;
				synchronized(flightServer){
					result = flightServer.addFlight(id, flightNum, flightSeats, flightPrice);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("deleteFlight".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int flightNum = ((Integer)args.get(1)).intValue();
				boolean result;
				synchronized(flightServer){
					result = flightServer.deleteFlight(id, flightNum);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("queryFlight".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int flightNumber = ((Integer)args.get(1)).intValue();
				int result;
				synchronized(flightServer){
					result = flightServer.queryFlight(id, flightNumber);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if(("queryFlightPrice".equals(methodName))&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int flightNumber = ((Integer)args.get(1)).intValue();
				int result;
				synchronized(flightServer){
					result = flightServer.queryFlightPrice(id, flightNumber);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("reserveFlight".equals(methodName)&&(args.size()==3)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				int flightNumber = ((Integer)args.get(2)).intValue();
				boolean result;
				synchronized(flightServer){
					result = flightServer.reserveFlight(id, customer, flightNumber);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("deleteReservation".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				boolean result;
				synchronized(flightServer){
					result = flightServer.deleteReservation(id, customer);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("calculateBill".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				int result;
				synchronized(flightServer){
					result = flightServer.calculateBill(id, customer);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			
			//None worked
			if(reply!=null){
				return reply;
			}
			else{
				return new Reply(Reply.STATUS_METHOD_NOT_FOUND,new LinkedList<Object>());
			}
			
		} catch(Exception e){
			e.printStackTrace();
			return new Reply(Reply.STATUS_BAD_REQUEST,new LinkedList<Object>());
		}
	}
	
	public static void main(String[] args){
		if(args.length!=1){
			System.out.println("Specify port to run the server on as first argument");
			System.exit(0);
		}
		int port = 0;
		try{
			port = Integer.parseInt(args[0]);
		} catch(NumberFormatException e){
			System.out.println("Specify port to run the server on as first argument");
			System.exit(0);
		}
		
		FlightServer server;
		try {
			server = new FlightServer(port);
			server.start();
		} catch (IOException e) {
			System.err.println("Server was unable to start");
			e.printStackTrace();
		}
	}

}
